"use strict";
cc._RF.push(module, 'fe6cb4R8otFK7d6Aqscj248', 'Bezier');
// scripts/modes/base/Bezier.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var _a = cc._decorator, ccclass = _a.ccclass, executeInEditMode = _a.executeInEditMode, property = _a.property;
//电子邮件puhalskijsemen@gmail.com
//源码网站 开vpn打开 http://web3incubators.com/
//电报https://t.me/gamecode999
var Bezier = /** @class */ (function (_super) {
    __extends(Bezier, _super);
    function Bezier() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.p1Tracker = null;
        _this.p2Tracker = null;
        _this.random = false;
        _this.debug = false;
        _this.p1 = null;
        _this.c1 = null;
        _this.c2 = null;
        _this.p2 = null;
        _this.g = null;
        return _this;
    }
    Bezier.prototype.onLoad = function () {
        this.g = this.getComponent(cc.Graphics);
        this.p1 = this.node.getChildByName('p1');
        this.c1 = this.node.getChildByName('c1');
        this.c2 = this.node.getChildByName('c2');
        this.p2 = this.node.getChildByName('p2');
        if (!CC_EDITOR) {
            this.p1.active = false;
            this.c1.active = false;
            this.c2.active = false;
            this.p2.active = false;
        }
    };
    Bezier.prototype.startMove = function (node, time, callback, obsv) {
        if (callback === void 0) { callback = null; }
        if (obsv === void 0) { obsv = null; }
        if (!node)
            return;
        var points = this.getBezierPoints(node);
        node.setPosition(points[0]);
        cc.tween(node)
            .bezierTo(time, points[1], points[2], points[3])
            .call(function () {
            if (callback)
                callback.call(obsv, node);
        })
            .start();
    };
    Bezier.prototype.getBezierPoints = function (node) {
        var p1 = this.convertToNodeSpace(node, this.p1);
        var c1 = this.convertToNodeSpace(node, this.c1);
        var c2 = this.convertToNodeSpace(node, this.c2);
        var p2 = this.convertToNodeSpace(node, this.p2);
        if (this.random) {
            var mc1 = this.calcMirrorD(p1, p2, c1);
            c1 = this.getRandomP(c1, mc1);
            var mc2 = this.calcMirrorD(p1, p2, c2);
            c2 = this.getRandomP(c2, mc2);
        }
        return [cc.v2(p1.x, p1.y), cc.v2(c1.x, c1.y), cc.v2(c2.x, c2.y), cc.v2(p2.x, p2.y)];
    };
    Bezier.prototype.updateTracker = function () {
        if (this.p1Tracker) {
            var nodePos = this.convertToNodeSpace(this.p1, this.p1Tracker);
            this.p1.setPosition(nodePos);
        }
        if (this.p2Tracker) {
            var nodePos = this.convertToNodeSpace(this.p2, this.p2Tracker);
            this.p2.setPosition(nodePos);
        }
    };
    Bezier.prototype.update = function () {
        if (CC_EDITOR) {
            // 编辑器下
            this.updateTracker();
            this.draw();
        }
        else {
            // 运行环境
            if (this.debug) {
                this.draw();
            }
        }
    };
    Bezier.prototype.draw = function () {
        if (!this.debug && !CC_EDITOR)
            return;
        if (!this.p1 || !this.c1 || !this.c2 || !this.p2) {
            this.g.clear();
            return;
        }
        var p1 = this.p1.position;
        var c1 = this.c1.position;
        var c2 = this.c2.position;
        var p2 = this.p2.position;
        this.g.clear();
        this.g.moveTo(p1.x, p1.y);
        this.g.bezierCurveTo(c1.x, c1.y, c2.x, c2.y, p2.x, p2.y);
        this.g.stroke();
    };
    /**
     * 计算p2在p1坐标系中的坐标
     */
    Bezier.prototype.convertToNodeSpace = function (p1, p2) {
        return p1.parent.convertToNodeSpaceAR(p2.parent.convertToWorldSpaceAR(p2.position));
    };
    /**
     * 已知AB构成一个向量，求C在向量AB上的镜像点D
     */
    Bezier.prototype.calcMirrorD = function (A, B, C) {
        var AB = B.sub(A);
        var AC = C.sub(A);
        var NAB = AB.normalize();
        var NAC = AC.normalize();
        var cT = NAB.dot(NAC);
        var tyLen = AC.len() * cT;
        var AE = AB.normalize().mul(tyLen);
        var E = A.add(AE);
        var CE = E.sub(C);
        var CE2 = CE.mul(2);
        var D = C.add(CE2);
        return D;
    };
    /**
     * 从AB两个点构成的一条线段中，随机获取一个点
     */
    Bezier.prototype.getRandomP = function (A, B) {
        var AB = B.sub(A);
        var RAC = AB.mul(Math.random());
        return A.add(RAC);
    };
    __decorate([
        property({ type: cc.Node, tooltip: '起始点追踪器' })
    ], Bezier.prototype, "p1Tracker", void 0);
    __decorate([
        property({ type: cc.Node, tooltip: '终止点追踪器' })
    ], Bezier.prototype, "p2Tracker", void 0);
    __decorate([
        property({ tooltip: '是否随机' })
    ], Bezier.prototype, "random", void 0);
    __decorate([
        property({ tooltip: '运行时显示Bezier曲线' })
    ], Bezier.prototype, "debug", void 0);
    Bezier = __decorate([
        ccclass,
        executeInEditMode
    ], Bezier);
    return Bezier;
}(cc.Component));
exports.default = Bezier;

cc._RF.pop();